import utils_index from '../../utils/index.js';
const regeneratorRuntime = utils_index.regeneratorRuntime
import {
  walletModel
} from '../../apis/index.js'

Page({

  data: {
    wallet_info:{}
  },
  mixins: [require('../../myMixin.js')],
  async submitFormId(e) {
    let data = await getApp().submitFormId(e.detail.formId)
  },
  getMoneyInput(e){
    let value = e.detail.value
    let reg =  /^([1-9]\d{0,9}|0)([.]?|(\.\d{1,2})?)$/;
    if(!value||reg.test(value)){
      this.setData({
        balance:value
      })
    }else{
      this.setData({
        balance:this.data.balance
      })
    }
  },
  async getUserInfo(res) {
    if (res.detail.encryptedData) {
      utils_index.showLoading('授权中...')
      let data = await contentModel.holdInfo({
        avatar: res.detail.userInfo.avatarUrl,
        name: res.detail.userInfo.nickName
      })

      if (data) {
        getApp().loginShow = true
        this.setData({
          loginShow: getApp().loginShow
        })
      }
      utils_index.hideAll()
    }
  },

  async getData() {
    let infos = await walletModel.getUserInfo()
    this.setData({
      infos,
      miniWithdrawalText: `最低${infos.miniWithdrawal}起提`
    })
  },
  async deposit(){
    let { minRange} = this.data.wallet_info
    let {balance} = this.data
    if (!balance || balance <= 0 || isNaN(balance)){
      return
    }
    let show = await utils_index.showModalContent('是否提现','是',true,'否')
    if (!show) return

    if (balance < minRange){
      utils_index.showToast(`最低金额${minRange}元`,'none')
      return
    }
    let infos = await walletModel.balanceWithdrawal({ balance})
    // console.log('tixianchenggong', infos)
    if (infos){
      let show = await utils_index.showModalContent('提现成功','','确定',false)
      this.setData({
        modelShow:false
      },()=>{
          this.getData()
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    // let data = await contentModel.holdInfo2()
    // if (!data) {
    //   getApp().loginShow = false
    // }
    // this.setData({
    //   loginShow: getApp().loginShow,
    //   prefixString: utils_index.prefixString
    // })
    // this.getData()
    this.getUserWallet()
  },
  getUserWallet(){
    walletModel.getUserInfo().then(res=>{
      let {balance,miniWithdrawal:minRange,free_rate:rate} = res
      this.setData({
        wallet_info:{
          balance:balance,
          minRange:minRange,
          rate:rate*100
        }
      })
    })
  },
  goUrl(e){
    let url = e.currentTarget.dataset.url
    utils_index.goUrl(url)
  }
})